@extends('layout.master')

@section('title','游戏奖励')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')

    <form id="test-form" class="mfp-hide white-popup-block floating-labels season-reward-add"
          enctype="multipart/form-data"
          method="post">
        <h4>添加奖励</h4>
        <hr>
        <div class="form-group m-b-40 m-t-10">
            <div class="form-group m-b-40 m-t-10">
                <input type="text" class="form-control" id="reward-title" name="title"
                       required><span
                        class="highlight"></span> <span class="bar"></span>
                <label for="title">奖品名称</label>
            </div>
            <div class="form-group m-b-40 m-t-10">
                <input type="text" class="form-control" id="ranking" name="ranking"
                       required><span
                        class="highlight"></span> <span class="bar"></span>
                <label for="title">名次</label>
            </div>
            <div class="form-group m-b-40 m-t-10">
                <input type="text" class="form-control" id="start" name="start"
                       required><span
                        class="highlight"></span> <span class="bar"></span>
                <label for="start">更换时间<span style="color: red; font-size: 10px;">*格式 2018-12-01 08:00:00</span></label>
            </div>
            <div class="form-group m-b-40 m-t-10">
                <input type="file" id="reward-img" class="dropify" name="reward_img"
                       data-height="200px"/>
                <label for="reward-img">奖品图片</label>
            </div>
            <div class="form-group m-b-40 m-t-10">
                <textarea name="contents" class="form-control" id="summernote" cols="30"
                          rows="5"></textarea>
                <label for="summernote">奖品介绍</label>
            </div>
        </div>
        <div class="form-actions">
            <div class="row">
                <div class="col-md-12">
                    <div class="row">
                        <div class="col-md-offset-3 col-md-9">
                            <button type="submit" class="btn btn-success reward-add"><i
                                        class="fa fa-check"></i> 添加
                                {!! csrf_field() !!}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <div class="panel">
        <div class="panel-heading">
            <div class="panel-action" style="margin-top: -9px">
                <a class="popup-with-form btn fcbtn btn btn-outline btn-info btn-1c" href="#test-form">
                    <span>添加奖励</span>
                </a>
            </div>
        </div>
        <div class="panel-heading">游戏奖励列表</div>
        <div class="panel-wrapper collapse in" aria-expanded="true">
            <div class="panel-body">
                <div class="table-responsive">
                    <table id="example23" class="display nowrap" cellspacing="0" width="100%"
                           data-url="{{ url(request()->getUri()) }}">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>名次</th>
                            <th>展示图</th>
                            <th>状态</th>
                            <th>开始使用时间</th>
                            <th>删除</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('js')
    @parent
    <script src="{{ asset('js/myself.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup-init.js') }}"></script>

    <script>

        $(".reward-add").click(function (e) {
            e.preventDefault();
            var title = $("#reward-title").val();
            var ranking = $("#ranking").val();
            var start_at = $("#start").val();
            var reward_img = document.getElementById("reward-img").files;
            var detail = $("#summernote").val();
            if (!reward_img.length) {
                swal("请上传图片!");
                return false;
            }
            if ((reward_img[0].type).indexOf("image/") === -1) {
                swal("请上传图片!");
                return false;
            }
            var img = picajax(reward_img[0]);
            swal({
                    title: "",
                    text: "确定添加么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({

                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/runGame/reward/add",
                        data: {ranking: ranking, title: title, start_at: start_at, img: img, detail: detail},
                        type: "POST",
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });

                            }
                        }
                    })
                }
            );
        });


        obj.on('click', '.btn-del', function () {
            var id = $(this).data('id');
            swal({
                    title: "",
                    text: "确定删除么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: '/runGame/reward/del/' + id,
                        type: 'POST',
                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            }
                        }
                    })
                }
            );
        });
    </script>
@endsection